﻿
using PW.Common;
using PW.Api.Api;
using PW.Api.Model;
using PW.SystemSet.Views;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Collections.Generic;

namespace PW.SystemSet.ViewModel
{

    public class SysUserViewModel : INotifyPropertyChanged
    {
        //构造函数
        public SysUserViewModel()
        {
            QueryCommand = new RelayCommand(QueryCommandFunc);
            SubmitCommand = new RelayCommand(SubmitCommandFunc);
            NextPageSearchCommand = new RelayCommand(NextPageSearchCommandFunc);
            SelectAllCommand = new RelayCommand(SelectAllCommandFunc);
            UnSelectAllCommand = new RelayCommand(UnSelectAllCommandFunc);
            AddCommand = new RelayCommand(AddCommandFunc);
            DeleteCommand = new RelayCommand(DeleteCommandFunc);
            ModifyCommand = new RelayCommand(ModifyCommandFunc);
            InfoCommand = new RelayCommand(InfoCommandFunc);
            GetData();
        }

        #region 查询相关属性
        private SysUser _queryObj = new SysUser();
        /// <summary>
        /// Username
        ///</summary>
        public SysUser QueryObj
        {
            get { return _queryObj; }
            set
            {
                _queryObj = value;
                RaisePropertyChanged("QueryObj");
            }
        }
        
        public RelayCommand QueryCommand { get; set; }

        private void QueryCommandFunc()
        {
            CurrentPage = 1;
            GetData();
        }

        //数据源
        ObservableCollection<DataGridModel<SysUser>> _list = new ObservableCollection<DataGridModel<SysUser>>();
        public ObservableCollection<DataGridModel<SysUser>> list
        {

            get { return _list; }
            set
            {
                _list = value;
                RaisePropertyChanged("list");
            }
        }
        private void GetData()
        {
            list.Clear();
            var pageIndex = Convert.ToInt32(CurrentPage);
            _queryObj.pageNum = pageIndex;
            _queryObj.pageSize = PageSize;
            _queryObj.orderByColumn = "user_id";
            _queryObj.isAsc = "asc";
            ResponseContent res = UserApi.listUser(_queryObj);
            if (res.isSuccess()) {
                List<SysUser> rows = res.getRows<SysUser>();
                this.TotalPage = 999;
                this.TotalCount = res.total;
                foreach (SysUser item in rows)
                {
                    list.Add(new DataGridModel<SysUser>() { IsChecked = false, ObjData = item });
                }
            }
        }
        #endregion

        #region 分页相关属性
        /// <summary>
        /// 分页管理
        ///</summary>
        public RelayCommand NextPageSearchCommand { get; set; }
        /// <summary>
        /// 分页查询命令
        ///</summary>
        private void NextPageSearchCommandFunc()
        {
            GetData();
        }
        private int _totalPage = 0;
        /// <summary>
        /// 总页数
        ///</summary>
        public int TotalPage
        {
            get { return _totalPage; }
            set
            {
                _totalPage = value;
                RaisePropertyChanged("TotalPage");
            }
        }

        private int _currentPage = 1;
        /// <summary>
        /// 当前页
        ///</summary>
        public int CurrentPage
        {
            get { return _currentPage; }
            set
            {
                _currentPage = value;
                RaisePropertyChanged("CurrentPage");
            }
        }

        private int _pageSize = 10;
        /// <summary>
        /// 每页显示的记录数
        ///</summary>
        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                _pageSize = value;
                RaisePropertyChanged("PageSize");
            }
        }
        private int _totalCount;
        public int TotalCount
        {
            get { return _totalCount; }
            set
            {
                _totalCount = value;
                RaisePropertyChanged("TotalCount");
            }
        }
        #endregion

        #region form
        private bool _IsAdd = true;
        /// <summary>
        /// 
        /// </summary>
        public bool IsAdd
        {
            get { return _IsAdd; }
            set
            {
                _IsAdd = value;
                RaisePropertyChanged("IsAdd");
            }
        }

        private SysUser _formData = new SysUser();
        /// <summary>
        /// _formData
        ///</summary>
        public SysUser FormData
        {
            get { return _formData; }
            set
            {
                _formData = value;
                RaisePropertyChanged("FormData");
            }
        }

        public RelayCommand SubmitCommand { get; set; }

        private void SubmitCommandFunc()
        {
            ResponseContent res = null;
            if (_IsAdd) {
                res = UserApi.addUser(_formData);
            } else {
                res = UserApi.updateUser(_formData);
            }
            if (res != null) {
                if (res.isSuccess())
                {
                    MessageBoxX.Success("提交成功！");
                }
                else {
                    MessageBoxX.Error(res.msg);
                }
            }
        }
        #endregion

        #region datagrid全选
        private bool? _IsCheckedAll = false;
        /// <summary>
        ///
        ///</summary>
        public bool? IsCheckedAll
        {
            get { return _IsCheckedAll; }
            set
            {
                _IsCheckedAll = value;
                RaisePropertyChanged("IsCheckedAll");
            }
        }

        public RelayCommand SelectAllCommand { get; set; }
        private void SelectAllCommandFunc()
        {
            foreach (DataGridModel<SysUser>
              item in list)
            {
                item.IsChecked = true;
            }
        }
        public RelayCommand UnSelectAllCommand { get; set; }
        private void UnSelectAllCommandFunc()
        {
            foreach (DataGridModel<SysUser> item in list)
            {
                item.IsChecked = false;
            }
        }

        List<SysUser> getSelections() {
            List<SysUser> selections = new List<SysUser>();
            foreach (DataGridModel<SysUser> item in list)
            {
                if (item.IsChecked) {
                    selections.Add(item.ObjData);
                }
            }
            return selections;
        }

        List<long> getSelectionIds()
        {
            List<long> ids = new List<long>();
            foreach (DataGridModel<SysUser> item in list)
            {
                if (item.IsChecked)
                {
                    ids.Add(item.ObjData.userId.Value);
                }
            }
            return ids;
        }
        #endregion

        #region 增删查改命令
        public RelayCommand AddCommand { get; set; }
        private void AddCommandFunc()
        {
            _IsAdd = true;
            _formData = new SysUser();
            SysUserForm edit = new SysUserForm(this);
            edit.ShowDialog();
        }
        public RelayCommand DeleteCommand { get; set; }
        private void DeleteCommandFunc()
        {
            List<long> ids = getSelectionIds();
            if (ids != null && ids.Count > 0)
            {
                // 支持多选
                var qures = MessageBoxX.Question(String.Format("确认删除所选{0}条数据？", ids.Count));
                if (qures) {
                    ResponseContent res = UserApi.delUser(ids);
                    if (res != null)
                    {
                        if (res.isSuccess())
                        {
                            MessageBoxX.Success("删除成功！");
                        }
                        else
                        {
                            MessageBoxX.Error(res.msg);
                        }
                    }
                }
            }
        }
        public RelayCommand ModifyCommand { get; set; }
        private void ModifyCommandFunc()
        {
            List<SysUser> selections = getSelections();
            if (selections != null && selections.Count > 0) {
                _IsAdd = false;
                _formData = selections[0];
                SysUserForm edit = new SysUserForm(this);
                edit.ShowDialog();
            }
        }
        public RelayCommand InfoCommand { get; set; }
        private void InfoCommandFunc()
        {
            List<SysUser> selections = getSelections();
            if (selections != null && selections.Count > 0)
            {
                _IsAdd = false;
                _formData = selections[0];
                SysUserForm edit = new SysUserForm(this);
                edit.ShowDialog();
            }
        }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
